package com.dzl.card;

import com.dzl.base.controller.BaseController;
import com.dzl.card.pojo.dto.WelfareCardSecretDto;
import com.dzl.card.pojo.query.WelfareCardSecretQuery;
import com.dzl.card.pojo.vo.WelfareCardSecretVo;
import com.dzl.card.service.WelfareCardSecretService;
import com.dzl.util.DateUtil;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.dzl.util.PageUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 卡密信息表(WelfareCardSecret)表控制层
 * @author caobucao
 * @since 2021-05-19 16:58:45
 */
@Api(value = "企业端-卡密信息管理", tags = "企业端-卡密信息管理")
@RestController
@RequestMapping("welfareCardSecret")
public class WelfareCardSecretController extends BaseController {

    @Autowired
    private WelfareCardSecretService service;

    /**
     * 分页查询卡密列表，卡密管理页用
     * @date 2021/5/19 17:12
     * @author caobucao
     * @param query 查询条件
     * @return com.dzl.util.PageUtils<com.dzl.cardsecret.entity.vo.WelfareCardSecretVo>
     */
    @ApiOperation(value = "企业端-分页查询卡密列表，卡密管理页用", notes = "企业端-分页查询卡密列表，卡密管理页用")
    @GetMapping("queryWelfareCardSecretVoListPage")
    public PageUtils<WelfareCardSecretVo> queryWelfareCardSecretVoListPage(WelfareCardSecretQuery query) {
        PageInfo<WelfareCardSecretVo> pageInfo = service.selectWelfareCardSecretVoListPage(query);
        return new PageUtils<>(pageInfo);
    }

    /**
     * 查询卡密--查询单条，福利卡管理页面的搜索用
     * @date 2021/7/30 15:27
     * @author caobucao
     * @param number 卡号
     * @param secret 卡密
     * @return com.dzl.util.JsonResult<com.dzl.cardsecret.pojo.vo.WelfareCardSecretVo>
     */
    @ApiOperation(value = "企业端-查询卡密--查询单条，福利卡管理页面的搜索用", notes = "企业端-查询卡密--查询单条，福利卡管理页面的搜索用")
    @GetMapping("queryWelfareCardSecretVoByNumberOrSecret")
    public JsonResult<WelfareCardSecretVo> queryWelfareCardSecretVoListPage(String number, String secret) {
        WelfareCardSecretVo welfareCardSecretVo = service.queryWelfareCardSecretVoByNumberOrSecret(number, secret);
        return JsonResult.success(welfareCardSecretVo);
    }

    /**
     * 批量激活、冻结、解冻卡密
     * @date 2021/5/19 18:41
     * @author caobucao
     * @param dto 传入参数
     * @return com.dzl.util.JsonResult<java.lang.Integer>
     */
    @ApiOperation(value = "企业端-批量激活、冻结、解冻卡密", notes = "企业端-批量激活、冻结、解冻卡密")
    @PostMapping("updateBatchCardSecretByIds")
    public JsonResult<String> updateBatchCardSecretByIds(@RequestBody WelfareCardSecretDto dto) {
        if(EmptyUtils.isEmpty(dto.getIds())) {
            return JsonResult.paramErrWithMsg("请选择要激活的卡密");
        }
        //如果是激活，才有到期时间
        if(dto.getIsActivate() == 1) {
            if(EmptyUtils.isEmpty(dto.getExpireDate())) {
                return JsonResult.paramErrWithMsg("请选择有效期！");
            }
            if(EmptyUtils.isEmpty(dto.getAmount())) {
                return JsonResult.paramErrWithMsg("请输入卡面额！");
            }
            /*if(EmptyUtils.isEmpty(dto.getCustomersId())) {
                return JsonResult.paramErrWithMsg("请选择客户！");
            }
            if(EmptyUtils.isEmpty(dto.getSalesId())) {
                return JsonResult.paramErrWithMsg("请选择业务员！");
            }*/
            Date expireTime = DateUtil.parseDateTime(dto.getExpireDate() + " 23:59:59");
            dto.setExpireTime(expireTime);
            dto.setActivateTime(new Date());
        }
        dto.setUpdater(getAdminInfo().getId());
        dto.setUpdaterName(getAdminInfo().getUserName());
        dto.setUpdateTime(new Date());
        dto.setEnterpriseId(getAdminInfo().getEnterpriseId());
        int count = service.updateBatchCardSecretByIds(dto);
        return count > 0 ? JsonResult.success("共操作"+count+"条数据，操作成功！") : JsonResult.paramErrWithMsg("批量操作失败！");
    }

    /**
     * 通过ids导出数据
     * @date 2021/5/20 9:41
     * @author caobucao
     * @param  ids 卡密的ids
     * @return com.dzl.util.JsonResult<java.util.List<com.dzl.cardsecret.entity.vo.WelfareCardSecretVo>>
     */
    @ApiOperation(value = "企业端-通过ids导出数据", notes = "企业端-通过ids导出数据")
    @GetMapping("exportCardSecretByIds")
    public JsonResult<List<WelfareCardSecretVo>> exportCardSecretByIds(String ids) {
        if(EmptyUtils.isEmpty(ids)) {
            return JsonResult.paramErrWithMsg("请选择要导出的数据！");
        }
        return JsonResult.success(service.selectWelfareCardSecretVoListByIds(ids));
    }

}
